﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeGenerator.Utils
{
    public static class Utils
    {

        public static string GenFullPath(string inputPath)
        {
            string fullPath = inputPath;

            // 如果输入路径是相对路径，则将其与当前目录组合以获取绝对路径
            if (!Path.IsPathRooted(inputPath))
            {
#if DEBUG
                string current = Directory.GetCurrentDirectory();
                fullPath = Path.Combine(current, inputPath);
#else
                string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                fullPath = Path.Combine(appDataFolder, "MybatisPlusGenerator",inputPath);
#endif
            }

            return fullPath;
        }

        public static void CopyDirectoryFiles(string source, string target)
        {
            source = GenFullPath(source);
            target = GenFullPath(target);
            // 创建目标文件夹（如果它不存在）
            Directory.CreateDirectory(target);

            // 获取源文件夹中的所有文件和子文件夹
            string[] files = Directory.GetFiles(source, "*", SearchOption.AllDirectories);

            // 复制所有文件和文件夹到目标文件夹
            foreach (string file in files)
            {
                string relativePath = file.Substring(source.Length + 1);
                ;
                string destinationPath = Path.Combine(target, relativePath);

                // 确保目标文件夹存在
                Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
                // 复制文件
                File.Copy(file, destinationPath, true);
            }
        }

        public static string ToSmallCamelCase(this string s)
        {
            // Remove any non-alphanumeric characters and underscores
            string cleanedColumnName = Regex.Replace(s, @"[^A-Za-z0-9]+", " ");

            // Split the string into words
            string[] words = cleanedColumnName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (words.Length == 0)
            {
                // If there are no valid words, return an empty string
                return string.Empty;
            }

            // Capitalize the first word and lowercase the rest
            string camelCaseName = words[0].ToLower(CultureInfo.InvariantCulture);
            for (int i = 1; i < words.Length; i++)
            {
                camelCaseName += char.ToUpper(words[i][0], CultureInfo.InvariantCulture) +
                                 words[i].Substring(1).ToLower(CultureInfo.InvariantCulture);
            }

            return camelCaseName;
        }

        public static string ToBigCamelCase(this string s)
        {
            s = s.ToSmallCamelCase();
            if (s.Length > 1)
            {
                return char.ToUpper(s[0]) + s.Substring(1);
            }
            else
            {
                return char.ToUpper(s[0]) + "";
            }
        }

        public static string UpperFirst(this string s)
        {
            if (s.Length > 1)
            {
                return char.ToUpper(s[0]) + s.Substring(1);
            }
            else
            {
                return char.ToUpper(s[0]) + "";
            }
        }

        public static string LowerFirst(this string s)
        {
            if (s.Length > 1)
            {
                return char.ToLower(s[0]) + s.Substring(1);
            }
            else
            {
                return char.ToLower(s[0]) + "";
            }
        }

        public static bool IsNullOrEmpty(this string s)
        {
            return s == null || s.Length == 0;
        }

        public static bool IsJavaPackageName(this string input)
        {
            // 使用正则表达式来匹配Java包名规范
            string pattern = "^[a-z][a-z0-9_]*(\\.[a-z0-9_]+)*$";
            return Regex.IsMatch(input, pattern);
        }

        public static bool IsNotNullAndNotEmpty(this string s)
        {
            return s != null && s.Length > 0;
        }

        public static void DeleteDirectory(string path)
        {
            foreach (string file in Directory.GetFiles(path))
            {
                File.Delete(file);
            }

            foreach (string subDirectory in Directory.GetDirectories(path))
            {
                DeleteDirectory(subDirectory);
            }

            Directory.Delete(path);
        }
    }
}